#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2018/10/11 11:53
@desc
"""
import sys, os

retry_limit = 3
retry_count = 0
account_file = 'account.txt'
lock_file = 'account_lock.txt'

while retry_count < retry_limit:  # 只要重试不超过3次就不断循环
    username = raw_input('\033[32;1mUserName:\033[0m')
    lock_check = file(lock_file)  # 当用户输入用户名后，打开LOCK文件以检查是否此用户已经LOCK了

    for line in lock_check.readlines():
        if username == line:
            sys.exit('\033[31;1mUser %s is locked!\033[0m' % username)
            # 如果LOCK了就直接退出

    password = raw_input('\033[32;1mPassword:\033[0m')

    f = file(account_file, 'rb')  # 打开账号文件
    match_flag = False  # 默认为False，如果用户match上了，就设置为True
    for line in f.readlines():
        user, passwd = line.strip('\n').split()
        # 去掉每行多余的\n，并把这一行按空格分成两列，分别赋值给user和passwd两个变量
        if username == user and password == passwd:  # 判断用户名和密码是否都相等
            print('Match!', username)
            match_flag = True  # 相等就把循环外的match_flag变量改为True
            break  # 然后就不用循环了，直接跳出，因为已经match上了

    f.close()
    if not match_flag:
        # 如果match_flag还为False，代表上面的循环中根本就没有match上用户名和密码，所以还需要循环
        print('User unmatched')
        retry_count += 1
    else:
        print('Welcome login OldBoy Learning system!')
else:
    print('Your account is locked!')
    f = file(lock_file, 'ab')
    f.write(username + '\n')
    f.close()
